<?php
/**
 *
 * 作    者：安德玛
 * 版本控制：
			２０２１０２０９－－－－－优化了提现页面得显示
 * 功能说明：用户提现控制器。
 *
 **/

namespace Admin\Controller;

class TixianController extends ComController
{
    /**
     * 列表
     */
    public function index()
    {
        $key = I('get.keyword','','htmlspecialchars');
        $tx_date = I('get.tx_date','','htmlspecialchars');
        $status = I('get.status','','htmlspecialchars');
        $p = isset($_GET['p']) ? intval($_GET['p']) : '1';
        $field = I('get.field','','htmlspecialchars');
        $sort = I('get.sort','','htmlspecialchars');
        $m = M('tixian');
        
        if($status){
            $where['a.status'] = $status;
        }
        
        if($key){
            $where_bear['_logic'] = 'or';
            $where_bear['b.username'] = array('like','%'.$key.'%');
            $where_bear['b.nickname'] = array('like','%'.$key.'%');
            $where['_complex'] = $where_bear;
        }
        
        if($tx_date){
            $t_start = strtotime($tx_date.' 00:00:00');
            $t_end = strtotime($tx_date.' 23:59:59');
            $where['a.t'] = array('between',array($t_start,$t_end));
        }
        
        $sortSql = "a.id desc";
        if($field and $sort){
            if($field == 'nickname'){
                $sortSql = "b.".$field." ".$sort;
            }else{
                $sortSql = "a.".$field." ".$sort;
            }
        }
        
        $pagesize =20;#每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $count = $m
                   ->alias("a")
                   ->join("LEFT JOIN ".C('DB_PREFIX')."user b on a.uid=b.id")
                   ->where($where)
                   ->count();
        $list = $m
                   ->alias("a")
                   ->join("LEFT JOIN ".C('DB_PREFIX')."user b on a.uid=b.id")
                   ->field("a.*,b.nickname")
                   ->order($sortSql)
                   ->where($where)
                   ->limit($offset . ',' . $pagesize)
                   ->select();
        foreach ($list as $k=>&$v){
            // 打款账户
            $user_payment = M('user_payment')->where('uid='.$v['uid'])->find();
            $list[$k]['payment'] = $user_payment;
			//提现金额
			$id = $v['uid'];
			$tixian_income = M('tixian')->where(array('uid'=>$id,'status'=>2))->sum('tixian_money');
			if(!$tixian_income){
				$tixian_income = 0;
			}
			$list[$k]['tixian_income'] =$tixian_income;
			//邀请奖励，登录，充值，提现，余额
			$user_info = M('user')->where('id='.$id)->field('money_invite,count,mylevel_num,cash_num,money')->find();
			$list[$k]['money_invite'] =$user_info['money_invite'];
			$list[$k]['count'] =$user_info['count'];
			$list[$k]['mylevel_num'] =$user_info['mylevel_num'];
			$list[$k]['cash_num'] =$user_info['cash_num'];
			$list[$k]['money'] =$user_info['money'];
        }
        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        $this->assign('list', $list);
        $this->assign('page', $page);
        $this->assign('status',$status);
        $this->assign('p',$p);
        $this->assign('field',$field);
        $this->assign('sort',$sort);
        $this->display();
    }
    
    /**
     * 审核
     */
    public function audit()
    {
        $id = I('id');
        $p = I('p',1);
        if(!$id){
            $this->error('传参为空');
        }
        if(IS_POST){
            $data['status'] = I('post.status','','htmlspecialchars');
            $data['remark'] = I('post.remark','','htmlspecialchars');
            if(!$data['status']){
                $this->error('审核状态不能为空');
            }
            $data['opt_id'] = $this->USER['uid'];
            $data['opt_t'] = time();
            $res = M('tixian')->data($data)->where('id='.$id)->save();
            if($res){
                addlog('操作提现审核，ID：'.$id);
                $this->userTixianCheck($id,$data['status']);
                $this->success('操作成功',U('index',array('p'=>$p)));
            }else{
                $this->error('操作失败，请稍后再试！');
            }
        }else{
            $vo = M('tixian')->where('id='.$id)->find();
            // 用户
            $user = M('user')->where('id='.$vo['uid'])->field('nickname,username,money,money_invite')->find();
            $vo['nickname'] = $user['nickname'];
            $vo['username'] = $user['username'];
			
            $this->assign('vo',$vo);
            $this->assign('p',$p);
            $this->display('form');
        }
    }
    
    
    public function shenhe()
    {
        $id = I('id');
        $status = I('status');
        $p = I('p',1);
        if(!$id or !$status){
            $this->error('传参为空');
        }
        $data['status'] = $status;
        $data['opt_id'] = $this->USER['uid'];
        $data['opt_t'] = time();
        $res = M('tixian')->data($data)->where('id='.$id)->save();
        
        if($res){
            addlog('操作提现审核，ID：'.$id);
            $this->userTixianCheck($id,$data['status']);
            $this->success('操作成功',U('index',array('p'=>$p)));
        }else{
            $this->error('操作失败，请稍后再试！');
        }
        
    }
    
    /**
     * 用户提现确认
     * @param number $id
     * @param number $status
     */
    private function userTixianCheck($id=0,$status=0)
    {
        $tixian = M('tixian')->where('id='.$id)->find();
        if($status == 3){
            //审核未通过，用户余额恢复
            M('user')->where('id='.$tixian['uid'])->setInc('money',$tixian['tixian_money']);
            //加入消息通知
            M('user_message')->data(array('uid'=>$tixian['uid'],'content'=>'提现金额'.$tixian['tixian_money'].'元，审核未通过','t'=>time(),'type'=>2))->add();
        }else {
            // 加入会员动态（提现成功）
            $user = M('user')->where('id='.$tixian['uid'])->find();
            M('user_dynamic')->data(array('mobile'=>$user['username'],'content'=>'提现成功','t'=>time()))->add();
            //加入消息通知
            M('user_message')->data(array('uid'=>$tixian['uid'],'content'=>'提现金额'.$tixian['tixian_money'].'元，已成功到账','t'=>time(),'type'=>2))->add();
        }
    }
	/**
	     * 体现审核备注
	     */
	    public function updateShenhe()
	    {
	        $id = I('uid','','htmlspecialchars');
	        if(!$id){
	            $this->error('传参为空你麻痹得');
	        }
	        if(IS_POST){
	            $data['status'] = I('post.status','','htmlspecialchars');
	            $data['remark'] = I('post.remark','','htmlspecialchars');
	            $res = M('tixian')->where(array('id'=>$id))->data($data)->save();
	            if($res){
	                addlog('操作提现审核，ID：'.$id);
	                $this->userTixianCheck($id,$data['status']);
	               redirect(U('updateShenhe',array('uid'=>$id,'close'=>1)));
	               die(0);
	            }else{
	                $this->error('操作失败！');
	            }
	        }else{
	          
	            $vo = M('tixian')->where(array('id'=>$id))->find();
	            
	            $this->assign('vo',$vo);
	            $this->assign('p',$p); 
	            $this->assign('status',$status); 
	            $this->display('form1');
	        }
	    }
}